package com.cn.unarmed.commonutils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.unarmed.activity.R;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 公用函数
 */
public class AppUtils{

    static Toast myToast;

    /**
     * listView高度适配函数
     * @param listView 在LinerLayout下的listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            LinearLayout listItem =(LinearLayout) listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    /**
     * 生成一个4位的随机数
     * @return 随机数
     */
    public static String randomText() {
        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 4) {
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuilder sb = new StringBuilder();
        for (Integer i : set) {
            sb.append(i);
        }
        return sb.toString();
    }

    public static void logger(String tag,String msg){
        Log.i(tag,msg);
    }

    /**
     * listView高度适配函数
     * @param listView 在LinerLayout下的listView
     */
    public static void setListViewHeightFrameLayout(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            FrameLayout listItem =(FrameLayout) listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static void showToast(Context context, String text, boolean b) {
        View layout = LayoutInflater.from(context).inflate(R.layout.pop_alert,null);

        LinearLayout lLayout = (LinearLayout) layout.findViewById(R.id.ll_text);
        TextView textView = (TextView) layout.findViewById(R.id.textn);

        //textView.setTypeface(new SetFontStyleUtils(context).setFontsNew_Lt_Font());
        WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int pixels= outMetrics.widthPixels;

        //lLayout.setWidth(pixels);
        lLayout.setAlpha(0.8f);
        lLayout.setBackgroundResource(b ? R.color.toast_bg_black : R.color.toast_bg_red);
        textView.setText(text);

        ImageView imageView = (ImageView) layout.findViewById(R.id.iv_alert);
        imageView.setImageDrawable(layout.getResources().getDrawable(b ? R.mipmap.success : R.mipmap.alert));

        myToast = new Toast(context);
        myToast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.TOP, 0, 0);
        myToast.setDuration(Toast.LENGTH_SHORT);
        myToast.setView(layout);
        myToast.show();

    }
}
